package com.julang.check.bean;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.julang.check.utils.CommonData.DOUSPLITE;
/**
 * @author 赵自强
 * */
@ApiModel(value = "RepeatWordAnalysis", description = "PaperYY检测报告分析结果")
public class RepeatWordAnalysis {
	@ApiModelProperty(value = "重复词列表")
	private List<RepeatWord> list;
	@JsonIgnore
	private Map<String, RepeatWord> map = new HashMap<>();
	@ApiModelProperty(value = "重复词个数")
	private int wordCount = 0;
	@ApiModelProperty(value = "可以替换的个数")
	private int replaceCount = 0;
	@ApiModelProperty(value = "可替换词语所占百分比")
	private double replacePercent = 0;

	public int getReplaceCount() {
		return replaceCount;
	}

	public double getReplacePercent() {
		return replacePercent;
	}

	public int getWordCount() {
		return wordCount;
	}

	public RepeatWordAnalysis() {
	}

	public List<RepeatWord> getList() {
		this.convetToList();
		return list;
	}

	private void addRepeatWord(RepeatWord repeatWord) {
		this.map.put(repeatWord.getOrginWord(), repeatWord);
		wordCount += repeatWord.getRepeatTime();
		if (repeatWord.isCanPlace()) {
			replaceCount++;
			replacePercent += repeatWord.getRepeatPercent();
		}
	}

	public RepeatWord getRepeatWord(String key) {
		return this.map.getOrDefault(key, null);
	}

	public void pushRepeatWord(String word, String id) {
		wordCount++;
		if (this.map.containsKey(word)) {
			RepeatWord repeatWord = this.map.get(word);
			repeatWord.setRepeatTime(repeatWord.getRepeatTime() + 1);
			repeatWord.setRepeatPercent((float) repeatWord.getRepeatTime() / wordCount);
			repeatWord.setOrignSentenceId(repeatWord.getOrignSentenceId() + DOUSPLITE + id);
			return;
		}
		RepeatWord repeatWord = new RepeatWord(word, 1, 0, id);
		this.map.put(word, repeatWord);
	}

	private void initList() {
		if (list == null) {
			list = new ArrayList<>();
		} else {
			list.clear();
		}
	}

	public boolean addReplaceWord(String word, String replaceWord) {
		RepeatWord repeatWord = map.get(word);
		if (repeatWord == null) {
			return false;
		}
		repeatWord.convertCanReplace(replaceWord);
		return true;
	}

	public int convetToList() {
		this.initList();
		for (Map.Entry<String, RepeatWord> entry : this.map.entrySet()) {
			RepeatWord repeatWord = entry.getValue();
			repeatWord.setRepeatPercent(repeatWord.getRepeatTime() / (float) wordCount);
			list.add(repeatWord);
		}
		return list.size();
	}

	public static RepeatWordAnalysis parserFromFile(String filePath) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			return null;
		}
		RepeatWordAnalysis repeatWordAnalysis = new RepeatWordAnalysis();
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String str = null;
		while ((str = reader.readLine()) != null) {
			RepeatWord repeatWord = RepeatWord.build(str);
			if (repeatWord == null) {
				System.out.println("读取到了错误的字符串：" + str);
				continue;
			}
			repeatWordAnalysis.addRepeatWord(repeatWord);
		}
		reader.close();
		return repeatWordAnalysis;
	}

	public int saveToFile(String filePath) throws IOException {
		File file = new File(filePath);
		if (file.exists()) {
			// 如果已存在,删除旧文件
			file.delete();
		}
		file = new File(filePath);
		file.createNewFile();
		this.convetToList();
		// 遍历输出每行  设置输出文件的编码为utf-8
		PrintWriter pfp = new PrintWriter(file, "UTF-8");
		int a = 0;
		for (int i = 0; i < list.size(); i++, a++) {
			pfp.print(list.get(i).description() + "\n");
		}
		pfp.close();
		return a;
	}

}
